package com.mydroidchat.views;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Kadavr
 * Date: 04.01.12
 * Time: 20:42
 * To change this template use File | Settings | File Templates.
 */
public class SigningInDialogView extends JDialog {
    public SigningInDialogView(Frame owner) {
        super(owner, "Signing in...", true);
        //todo can't be closed
        add(new LoadingLabel(), BorderLayout.WEST);
        JLabel label = new JLabel("Please wait...");
        label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        add(label, BorderLayout.CENTER);
        pack();
    }
    
    private class LoadingLabel extends JLabel {
        int angle = 0;
        
        private LoadingLabel() {
            super();
            LoadingIcon icon = new LoadingIcon();
            setIcon(icon);
            new Timer(20, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    angle = (angle + 25) % 360;
                    repaint();
                }
            }).start();
        }
        
        private class LoadingIcon implements Icon {
            public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setStroke(new BasicStroke(5));
                int width = getIconWidth();
                int height = getIconHeight();
                g2d.setPaint(Color.BLACK);
                g2d.drawArc(x + 16, y + 16, width - 32, height - 32, angle, (angle + 360 - 60) % 360);
            }

            public int getIconWidth() {
                return 64;
            }

            public int getIconHeight() {
                return 64;
            }
        }
    }
}
